package com.smartlink.action;

import java.util.List;
import java.util.Map;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

import com.opensymphony.xwork2.ActionContext;
import com.opensymphony.xwork2.ActionSupport;
import com.smartlink.pojo.Clazz;
import com.smartlink.pojo.Student;
import com.smartlink.pojo.Teacher;
import com.smartlink.service.StudentService;
import com.smartlink.service.TeacherService;

public class UserAction extends ActionSupport {

	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;
	private static final Log log = LogFactory.getLog(UserAction.class);
	private TeacherService teacherService;
	private StudentService studentService;
	private String userType;
	private String name;
	private String phone;
	private String password;
	private String userNo;
	private Integer clazzId;

	public String getAddUserForm() {
		Map<String, Object> request = (Map<String, Object>) ActionContext.getContext().get("request");
		List<Clazz> clazzs = studentService.getClazzList();
		request.put("calzzs", clazzs);
		// 准备班级信息
		return "SUCCESS";
	}

	public String addUser() {
		boolean flag = true;
		if ("0".equals(userType)) {
			// 添加学生
			Student student = new Student(userNo, name, phone, password);
			log.info(student);
			Clazz clazz = studentService.getClazzById(clazzId);
			student.setClazz(clazz);
			flag = studentService.save(student);

			log.info("添加学生");
		} else {
			// 添加教师
			Teacher teacher = new Teacher(userNo, name, phone, password);
			flag = teacherService.save(teacher);
			log.info("添加教师");
		}
		if (flag) {
			addActionMessage("添加成功！");
			return "SUCCESS";
		} else {
			addFieldError("error", "学生或教师已存在");
			return "FAIL";

		}

	}

	public TeacherService getTeacherService() {
		return teacherService;
	}

	public void setTeacherService(TeacherService teacherService) {
		this.teacherService = teacherService;
	}

	public StudentService getStudentService() {
		return studentService;
	}

	public void setStudentService(StudentService studentService) {
		this.studentService = studentService;
	}

	public String getUserType() {
		return userType;
	}

	public void setUserType(String userType) {
		this.userType = userType;
	}

	public String getName() {
		return name;
	}

	public void setName(String name) {
		this.name = name;
	}

	public String getPhone() {
		return phone;
	}

	public void setPhone(String phone) {
		this.phone = phone;
	}

	public String getPassword() {
		return password;
	}

	public void setPassword(String password) {
		this.password = password;
	}

	public String getUserNo() {
		return userNo;
	}

	public void setUserNo(String userNo) {
		this.userNo = userNo;
	}

	public Integer getClazzId() {
		return clazzId;
	}

	public void setClazzId(Integer clazzId) {
		this.clazzId = clazzId;
	}

}
